/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

<template>
  <div class="rk-service-sla">
    <div class="grey">{{title}}</div>
    <div class="title b">
      <span>{{this.stateDashboard[this.type].getSLATrend.length ? content.toFixed(2) : 0}}</span>
      <span class="unit"> %</span>
    </div>
    <RkEcharts height="180px" :option="responseConfig"/>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
import Num from './chart-num.vue';

interface NumBoxData {
  value: number;
}
@Component({components: {Num}})
export default class Response extends Vue {
  @Prop() private title!: string;
  @Prop() private type!: string;
  @Prop() private stateDashboard!: any;
  @Prop() private intervalTime!: any;
  get content() {
    const temp = this.stateDashboard[this.type].getSLATrend.map((i: NumBoxData) => i.value).filter((i: any) => i);
    const sum = temp.length ? temp
    .reduce((preValue: number, curValue: number, index: number, array: number[]) => preValue + curValue)
    / this.stateDashboard[this.type].getSLATrend.length : 0;
    return sum / 100;
  }
  get avg() {
     return this.stateDashboard[this.type].getSLATrend ?
      [{
        data: this.stateDashboard[this.type].getSLATrend.map((i: any, index: number) => [
          this.intervalTime[index],
          i.value / 100,
        ]),
        name: this.stateDashboard[this.type].getSLATrend.length ? 'Avg Response' : null,
        type: 'bar',
        barMaxWidth: 10,
        // barCategoryGap: '-70%',
        // symbol: 'path://d="M150 50 L130 130 L170 130  Z"',
      }] : [];
  }
  get responseConfig() {
    return {
      color: [
        'rgba(63, 150, 227, 0.8)',
      ],
      grid: {
        top: 35,
        left: 0,
        right: 28,
        bottom: 0,
        containLabel: true,
      },
      tooltip: {
        trigger: 'axis',
        backgroundColor: 'rgb(50,50,50)',
        textStyle: {
          fontSize: 13,
        },
      },
      xAxis: {
        type: 'category',
        axisTick: {
          lineStyle: { color: '#c1c5ca41' },
          alignWithLabel: true,
        },
        splitLine: { show: false },
        axisLine: { lineStyle: { color: 'rgba(0,0,0,0)' } },
        axisLabel: { color: '#9da5b2', fontSize: '11' },
      },
      yAxis: {
        type: 'value',
        axisLine: { show: false },
        axisTick: { show: false },
        splitLine: { lineStyle: { color: '#c1c5ca41', type: 'dashed' } },
        axisLabel: { color: '#9da5b2', fontSize: '11' },
      },
      series: [
        ...this.avg,
      ],
    };
  }
}
</script>
<style lang="scss">
.rk-service-sla{
  padding: 0 10px;
  height: 258px;
  .unit{
    font-size: 16px;
  }
  .title{
    font-size: 24px;
  }
}
</style>